package org.seckill.common;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Elmer
 * @create 2017-08-04 15:26
 * @desc 自定义拦截器
 **/
public class My01Interceptor implements HandlerInterceptor {
    /**
     * 返回值：表示我们是否需要将当前的请求拦截下来
     * 返回true：请求继续进行
     * 返回false：请求将被终止
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o 表示被拦截请求的目标对象
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("执行了preHandle方法");
        return false;
    }

    /**
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView 可以通过ModelAndView参数来改变显示的视图，或修改发往视图的方法
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("执行了postHandle方法");
        //modelAndView.addObject("msg","被拦截器修改之后的消息");
        //modelAndView.setViewName("/hello.jsp");
    }


    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("执行了afterCompletion方法");
    }
}
